package com.iot.ruler.core;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 状态定制线程池
 * 监控线程执行情况
 *
 * @author lzb
 * @Date 2023/2/12 19:53
 */
public class StatusThreadPoolExecutor extends ThreadPoolExecutor {

    private final OnThreadExecuteListener listener;

    public StatusThreadPoolExecutor(int corePoolSize, int maximumPoolSize,
                                    long keepAliveTime, TimeUnit unit,
                                    BlockingQueue<Runnable> workQueue,
                                    CacheRejectHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, new RulerThreadFactory(), handler);
        handler.bind(this);
        this.listener = handler;
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        listener.beforeExecute(t, r);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        listener.afterExecute(r, t);
    }

    @Override
    protected void terminated() {
        listener.terminated();
    }

    @Override
    public void execute(Runnable command) {
        super.execute(command);
    }
}
